package etools

import (
    "reflect"
    "errors"

    "github.com/jinzhu/copier"
)

func CopyStruct(src interface{}, dest interface{}) error {
    srcType := reflect.TypeOf(src)
    destType := reflect.TypeOf(dest)
    if srcType.Kind() != reflect.Ptr || destType.Kind() != reflect.Ptr {
        return errors.New("src and desc must be point type")
    }

    srcType = srcType.Elem()
    destType = destType.Elem()
    if srcType.Kind() != reflect.Struct || destType.Kind() != reflect.Struct {
        return errors.New("src and desc must be struct point")
    }

    return copier.Copy(dest, src)
}

func CopyStructSlice(src interface{}, dest interface{}) error {
    srcType := reflect.TypeOf(src)
    destType := reflect.TypeOf(dest)
    if srcType.Kind() != reflect.Ptr || destType.Kind() != reflect.Ptr {
        return errors.New("src and desc must be slice type")
    }

    srcType = srcType.Elem()
    destType = destType.Elem()
    if srcType.Kind() != reflect.Slice || destType.Kind() != reflect.Slice {
        return errors.New("src and desc must be struct slice")
    }

    srcType = srcType.Elem()
    destType = destType.Elem()
    if srcType.Kind() != reflect.Struct || destType.Kind() != reflect.Struct {
        return errors.New("src and desc must be struct slice")
    }

    return copier.Copy(dest, src)
}
